有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java配置JAXB解组器来处理同时具有acessor方法和字段的类

是否可以配置JAXB上下文(用于解组器)本身,以便它可以为同时具有访问器getField()/setField()和字段的类A实例化上下文?A类不可修改:它是第三方或生成的

class A {
    public int field;
    public int getField();
    public void setField(int field);
}

标准JAXBContext实例化

   JAXBContext jaxbContext = JAXBContext.newInstance(A.class);

将给出以下例外情况

com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException: 1 counts of IllegalAnnotationExceptions

Class has two properties of the same name "field"
    this problem is related to the following location:
        at public int com.thecompany.A.getField()
        at com.thecompany.A
    this problem is related to the following location:
        at public int com.thecompany.A.field
        at com.thecompany.A

这可以通过使用

@XmlAccessorType(XmlAccessType.FIELD)

但这不是可接受的解决方案

有没有办法保持类定义不变,并且能够从XML文件中读取其实例?(可能不是JAXB?)

UPD:发现相同的问题得到了回答:JAX-B - How to map a schema element to an existing Java class

或者,在这个答案中使用http://wiki.eclipse.org/EclipseLink/Examples/MOXy/GettingStartedCreating Jaxb classes from xml without schema;nice教程在http://blog.bdoughan.com/2010/12/extending-jaxb-representing-annotations.html提供


共 (1) 个答案